using DevExpress.Utils;
using DevExpress.XtraBars.Alerter;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Menu;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraPrinting;
using Common.Class;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Common.Base
{
	public class XucBase : XtraUserControl
	{
		public delegate void SuccessEventHander(object sender, object item);
		public enum TypeAdd
		{
			None,
			Single,
			Replace,
			Add,
			Duplicate
		}
		protected AlertControl Alc;
		protected BaseView ExportView;
		protected CloseOrNew MCloseOrNew;
		protected MenuButton MMenu;
		protected ProgressForm ProgressForm;
		private WaitDialogForm _dlg;
		private string _title = "";
		protected Actions MStatus;
		public event SaveChangedEventHander SaveChanged;
		[Category("Config"), Description("Là thuộc tính thể trạng thái của dữ liệu hiện tại có được lưu hay chưa."), DisplayName("Save")]
		public bool NotSave
		{
			get;
			set;
		}
		[Category("Config"), Description("Là thuộc tính thể trạng thái của dữ liệu."), DisplayName("Status")]
		public Actions Status
		{
			get
			{
				return this.MStatus;
			}
			set
			{
				this.MStatus = value;
			}
		}
		[Category("Config"), DisplayName("Close")]
		public CloseOrNew IsClose
		{
			get
			{
				return this.MCloseOrNew;
			}
			set
			{
				this.MCloseOrNew = value;
			}
		}
		[Category("Config"), Description("Là thuộc tính tiêu đề khi in ra."), DisplayName("Title")]
		public string Title
		{
			get
			{
				return this._title;
			}
			set
			{
				this._title = value;
			}
		}
		[Category("Config"), DisplayName("Bar"), PropertyTab(typeof(MenuButton))]
		public MenuButton RibbonBar
		{
			get
			{
				return this.MMenu;
			}
			set
			{
				this.MMenu = value;
			}
		}
		public XucBase()
		{
			base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
			this.Alc = new AlertControl
			{
				AllowHtmlText = true
			};
			this.MStatus = Actions.None;
			this.MCloseOrNew = CloseOrNew.None;
			this.MMenu = new MenuButton();
		}
		public void CreateWaitDialog()
		{
			this._dlg = new WaitDialogForm("Đang nạp dữ liệu...", "Vui lòng đợi trong giây lát...");
		}
		public void SetWaitDialogCaption(string fCaption)
		{
			if (this._dlg != null)
			{
				this._dlg.Caption = fCaption;
			}
			else
			{
				this.CreateWaitDialog();
				if (this._dlg != null)
				{
					this._dlg.Caption = fCaption;
				}
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (this._dlg != null)
			{
				this._dlg.Close();
			}
		}
		protected void DoHide()
		{
			base.OnLoad(EventArgs.Empty);
			if (this._dlg != null)
			{
				this._dlg.FormClosing += new FormClosingEventHandler(this.DlgFormClosing);
				this._dlg.Close();
			}
		}
		private void DlgFormClosing(object sender, FormClosingEventArgs e)
		{
			this._dlg.Dispose();
			this._dlg = null;
		}
		public virtual void Clear()
		{
		}
		public string GenerateCode(string studentName)
		{
			string text = "";
			string[] array = studentName.Split(new char[]
			{
				' '
			});
			string[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				string text2 = array2[i];
				if (text2.Length > 0)
				{
					text += text2.Substring(0, 1);
				}
			}
			return text;
		}
		public void FormatColumns(RepositoryItemGridLookUpEdit item, string column, int pos, int width, string caption)
		{
			this.FormatColumns(item, column, pos, width, caption, true);
		}
		public void FormatColumns(RepositoryItemGridLookUpEdit item, string column, int pos, int width, string caption, bool visible)
		{
			this.FormatColumns(item, column, pos, width, caption, null, visible);
		}
		public void FormatColumns(RepositoryItemGridLookUpEdit item, string column, int pos, int width, string caption, RepositoryItem repositoryItem, bool visible)
		{
			GridColumn gridColumn = item.View.Columns.AddField(column);
			gridColumn.VisibleIndex = pos;
			gridColumn.Caption = caption;
			gridColumn.OptionsFilter.AutoFilterCondition = AutoFilterCondition.Contains;
			gridColumn.Visible = visible;
			gridColumn.ColumnEdit = repositoryItem;
			gridColumn.Width = width;
		}
		public object GetCellValue(Common.Class.RowClickEventArgs e, AdvBandedGridView view)
		{
			BandedGridColumn column = (e.ColumnIndex != -1) ? view.Columns[e.ColumnIndex] : view.Columns[e.FieldName];
			return view.GetRowCellValue(e.RowIndex, column);
		}
		public object GetCellValue(Common.Class.RowClickEventArgs e, GridView view)
		{
			GridColumn column = (e.ColumnIndex != -1) ? view.Columns[e.ColumnIndex] : view.Columns[e.FieldName];
			return view.GetRowCellValue(e.RowIndex, column);
		}
		public object GetCellValue(int rowindex, string fieldName, GridView view)
		{
			return this.GetCellValue(new Common.Class.RowClickEventArgs(rowindex, fieldName), view);
		}
		public object GetCellValue(int rowIndex, string fieldName, AdvBandedGridView view)
		{
			return this.GetCellValue(new Common.Class.RowClickEventArgs(rowIndex, fieldName), view);
		}
		protected virtual bool ExportPermision()
		{
			return true;
		}
		public virtual void Export(BaseView view)
		{
			this.ExportView = view;
			this.Export();
		}
		public void Export()
		{
			if (this.ExportPermision())
			{
				SaveFileDialog saveFileDialog = new SaveFileDialog
				{
					Filter = "Microsoft Excel 97-2003(*.xls)|*.xls|Microsoft Excel 2007(*.xlsx)|*.xlsx|PDF(*.pdf)|*.pdf|Rich Text Format(*.rtf)|*.rtf|Webpage (*.htm)|*.htm",
					FilterIndex = 0
				};
				saveFileDialog.ShowDialog();
				if (saveFileDialog.FileName != null)
				{
					try
					{
						if (saveFileDialog.FilterIndex == 1)
						{
							this.ExportToEx(saveFileDialog.FileName, "xls");
						}
						else
						{
							if (saveFileDialog.FilterIndex == 2)
							{
								this.ExportToEx(saveFileDialog.FileName, "xlsx");
							}
							else
							{
								if (saveFileDialog.FilterIndex == 3)
								{
									this.ExportToEx(saveFileDialog.FileName, "pdf");
								}
								else
								{
									if (saveFileDialog.FilterIndex == 4)
									{
										this.ExportToEx(saveFileDialog.FileName, "rtf");
									}
									else
									{
										if (saveFileDialog.FilterIndex == 5)
										{
											this.ExportToEx(saveFileDialog.FileName, "htm");
										}
									}
								}
							}
						}
						if (File.Exists(saveFileDialog.FileName))
						{
							if (XtraMessageBox.Show("Bạn muốn mở tài liệu này không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
							{
								Process.Start(saveFileDialog.FileName);
							}
						}
					}
					catch (Exception)
					{
						this.EndExport();
					}
				}
			}
		}
		protected void StartExport()
		{
			this.ProgressForm = new ProgressForm(this);
			this.ProgressForm.Show();
			this.ProgressForm.Refresh();
		}
		private void ExportToEx(string filename, string ext)
		{
			this.StartExport();
			Cursor current = Cursor.Current;
			Cursor.Current = Cursors.WaitCursor;
			if (this.ExportView == null)
			{
				this.EndExport();
				XtraMessageBox.Show("Đối tượng kết xuất chưa xác định");
			}
			else
			{
				//IPrintingSystem currentPS = PrintHelper.GetCurrentPS();
                IPrintingSystem currentPS = new PrintingSystem();
				currentPS.AfterChange += new ChangeEventHandler(this.Export_ProgressEx);
				if (ext == "rtf")
				{
					this.ExportView.ExportToRtf(filename);
				}
				if (ext == "pdf")
				{
					this.ExportView.ExportToPdf(filename);
				}
				if (ext == "mht")
				{
					this.ExportView.ExportToMht(filename);
				}
				if (ext == "htm")
				{
					this.ExportView.ExportToHtml(filename, new HtmlExportOptions("utf-8"));
				}
				if (ext == "txt")
				{
					this.ExportView.ExportToText(filename, new TextExportOptions(" ", Encoding.Unicode));
				}
				if (ext == "xls")
				{
					this.ExportView.ExportToXls(filename, new XlsExportOptions(TextExportMode.Value, true));
				}
				if (ext == "xlsOld")
				{
					this.ExportView.ExportToExcelOld(filename);
				}
				if (ext == "xlsx")
				{
					this.ExportView.ExportToXlsx(filename, new XlsxExportOptions(TextExportMode.Value, true));
				}
				currentPS.AfterChange -= new ChangeEventHandler(this.Export_ProgressEx);
				Cursor.Current = current;
				this.EndExport();
			}
		}
		protected void EndExport()
		{
			if (this.ProgressForm != null)
			{
				this.ProgressForm.Dispose();
			}
			this.ProgressForm = null;
		}
		protected void Export_ProgressEx(object sender, ChangeEventArgs e)
		{
			if (e.EventName == "ProgressPositionChanged")
			{
				int progressValue = (int)e.ValueOf("ProgressPosition");
				this.ProgressForm.SetProgressValue(progressValue);
			}
		}
		public void DoShowMenu(GridHitInfo hi, GridView view)
		{
			if (hi.HitTest == GridHitTest.ColumnButton)
			{
				GridViewMenu gridViewMenu = new GridViewColumnButtonMenu(view);
				gridViewMenu.Init(hi);
				gridViewMenu.Show(hi.HitPoint);
			}
		}
	}
}
